/*
 * This file is part of the Try! Engine project.
 *
 * Copyright 2010-2011 Emanuele Bertoldi. All rights reserved.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * You should have received a copy of the modified BSD License along with this
 * program. If not, see <http://www.opensource.org/licenses/bsd-license.php>
 */

#include "application.h"
#include <joysticklistener.h>
#include <mouselistener.h>
#include <keyboardlistener.h>
#include <renderwindow.h>
#include <graphicsmanager.h>
#include <inputmanager.h>
#include <fsm.h>
#include <behavior.h>
#include <exception.h>

using namespace Try;

MessageName Application::Msg::started = "started";
MessageName Application::Msg::updateRequest = "updateRequest";
MessageName Application::Msg::exitRequest = "exitRequest";

Application::Application(
        Behavior* logic,
        RenderWindow* rw
        )
    : Object(rw ? rw->windowTitle() : "Try! Application"),
      m_retCode(0),
      m_exitRequest(false),
      m_logic(logic),
      m_renderWin(rw),
      m_lastUpdateTime(0)
{
    if (!m_renderWin)
        throw Exception("Invalid Try::RenderWindow");

    ((Listenable<RenderWindowListener>*)m_renderWin)->addListener(this);

    new GraphicsManager(m_renderWin);
    new InputManager();

    new KeyboardListener(this);
    new MouseListener(this);
    new JoystickListener(this);
    new FSM(this);

    if (m_logic)
        m_logic->watchObject(this);
}

Application::~Application()
{
    ((Listenable<RenderWindowListener>*)m_renderWin)->removeListener(this);

    m_renderWin = 0;

    delete graphics;
    delete inputs;
}

int Application::run()
{
    try {

        this->states()->reset();

        m_lastUpdateTime = glfwGetTime();

        this->notifyMessage(Message(Application::Msg::started, m_lastUpdateTime), 0);

        while (!m_exitRequest)
        {
            double current_time = glfwGetTime();
            double dt = current_time - m_lastUpdateTime;

            this->notifyMessage(Message(Application::Msg::updateRequest, dt), 0);

            m_lastUpdateTime = current_time;
        }

        return m_retCode;

    } catch (Exception e) {

        return e.retCode();
    }
}

void Application::exit(int ret_code)
{
    if (!m_exitRequest)
    {
        m_retCode = ret_code;
        m_exitRequest = true;

        this->notifyMessage(Message(Application::Msg::exitRequest, ret_code), 0);
    }
}

RenderWindow* Application::window() const
{
    return m_renderWin;
}

FSM* Application::states() const
{
    return static_cast<FSM*>(this->property(FSM::name));
}

void Application::onWindowClosed(int ret_code)
{
    this->exit(ret_code);
}
